package gameUi
{
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	
	import gameUi.Event.ChipChooseEvent;

	public class Chip extends NewChipObject
	{
		private var _colorType:int;
		private var _numType:int;
		
		private var shadow:DropShadowFilter;
		private var oldY:Number;
		private var isChoose:Boolean;//是否选中该筹码
		
		private var index:int;//筹码索引
		private var _chipTypeArray:Array;//筹码帧数组
		private var _chipNum:int;//筹码面值
		
		/**
		 * 筹码类
		 * @param colorType 颜色帧数
		 * @param numType 面值帧数
		 * 
		 */
		public function Chip(colorType:int,numType:int,indexs:int,chipNum:int)
		{
			_colorType = colorType;
			_numType = numType;
			
			this.pchip.gotoAndStop(colorType);
			this.pchip.chipNumber.gotoAndStop(numType);
			index = indexs;
			_chipTypeArray = [colorType,numType];
			_chipNum = chipNum;
			
			chipInit();
			addListner();
		}

		private function chipInit():void{
			shadow = new DropShadowFilter(4,90);
			this.filters = [shadow];
			oldY = this.y;
		}
		
		private function addListner():void{
			this.addEventListener(MouseEvent.ROLL_OVER,overThis);
			this.addEventListener(MouseEvent.ROLL_OUT,outThis);
			this.addEventListener(MouseEvent.CLICK,chooseThis);
			this.addEventListener(Event.ENTER_FRAME,showChip);
		}
		
		//由于资源有问题，所以每一帧更新
		private function showChip(e:Event):void{
			if(!this.pchip) return;
			this.pchip.gotoAndStop(_colorType);
			this.pchip.chipNumber.gotoAndStop(_numType);
		}
		
		private function overThis(e:MouseEvent):void{
			if(!isChoose) TweenLite.to(this,0.3,{scaleY:1.2,y:-6});
		}
		
		private function outThis(e:MouseEvent):void{
			if(!isChoose) TweenLite.to(this,0.3,{scaleY:1,y:0});
		}
		//选中该筹码
		public function chooseThis(e:MouseEvent=null):void{
			if(isChoose) return;
			isChoose = true;
			TweenLite.killTweensOf(this);
			this.scaleY = 1;
			moveThis0();
			
			BlackJack.eventTower.dispatchEvent(new ChipChooseEvent(ChipChooseEvent.CHOOSE,index,_chipTypeArray,_chipNum));
		}
		//选中动画----------------------------------
		private function moveThis0():void{
			TweenLite.to(this,0.3,{y:oldY - 5,dropShadowFilter:{color:0, alpha:1, blurX:4, blurY:4, distance:8},onComplete:moveThis1});
		}
		
		private function moveThis1():void{
			TweenLite.to(this,0.3,{y:oldY,dropShadowFilter:{color:0, alpha:1, blurX:4, blurY:4, distance:4},onComplete:moveThis0});
		}
		//------------------------------------------
		/**
		 * 选择取消
		 **/
		public function chooseOver():void{
			isChoose = false;
			TweenLite.killTweensOf(this);
			this.y = oldY;
			this.filters = [shadow];
		}
		
		/**
		 * 筹码type值数组 [颜色帧,面值帧]
		 **/
		public function get chipTypeArray():Array
		{
			return _chipTypeArray;
		}
	}
}